﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Digitalmes.Application;
using Digitalmes.Common.Interfaces;

namespace Digitalmes.Infrastructure.IntegrationTests;

/// <summary>
/// 测试治具基类，提供必要的基础服务。
/// </summary>
public abstract class BaseTestFixture
{
    public IServiceProvider Services { get; }

    protected BaseTestFixture()
    {
        var hostBuilder = new HostBuilder();
        hostBuilder.ConfigureAppConfiguration((_, config) =>
        {
            config.AddJsonFile("appsettings.json");
        });
      
        hostBuilder.AddApplicationSetup();
        hostBuilder.AddInfrastructureSetup();

        hostBuilder.ConfigureServices((context, services) =>
        {
            Configure(services, context.Configuration);
            services.TryAddScoped<ICurrentUser>(_ => new MockCurrentUser());
        });

        var host = hostBuilder.Build();
        Services = host.Services;
    }

    /// <summary>
    /// 配置服务。
    /// </summary>
    /// <param name="services"></param>
    /// <param name="configuration"></param>
    protected virtual void Configure(IServiceCollection services, IConfiguration configuration)
    {
    }
}
